<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\member\controller;


use app\member\service\NodeService;
use app\common\facade\Arr;
use library\Controller;
use library\tools\Data;
use think\Console;
use think\Db;
use think\exception\HttpResponseException;
use app\common\facade\TransItem;
use app\common\services\JobService;
use app\common\services\PublishJobService;
use app\common\services\JobOptionService;

/**
 * 商家管理
 * Class Index
 * @package app\member\controller
 */
class Business extends Controller
{
    public $table = 'User';

    /**
     * 商家概况
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        //获取当前代理商所在城市

        $data['total_business']=DB::name($this->table)->where('type=2')->count();//商户总数
        $data['sucess_business']=DB::name($this->table)->where('type=2 and status=2 ')->count();//商户认证成功
        $data['pre_business']=DB::name($this->table)->where('status=1   and type=2')->count();
        $data['deny_business']=DB::name($this->table)->where('status=3   and type=2')->count();

        $this->assign('result',$data);
        $this->fetch();

    }

     /**
     * 商家列表
     * @auth true
     * @menu true
     * @throws \ReflectionException
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function businessList(){

        $this->title = '商家列表';
        $status = $this->request->param('status', 0);
        if($status!=0){
            $data[]=['status','EQ',$status];
            if($status==1){
                $this->title = '待认证商家';
            }elseif ($status==2) {
                $this->title = '审核通过商家列表';
            }else {
                $this->title = '审核拒绝商家列表';
            }
        }else{
            $data[]=['status','IN',[1,2,3,0]];
        }
        $data[]=['type','EQ',2];

       
        $query = $this->_query($this->table)
            ->where($data)
            ->like('title')
            ->equal('id,stauts,name,is_subscribe');
        $query->order('id desc')->page();



    }
   
    /**
     * 商家详情
     * @auth true
    */
    public function businessInfo(){
       
        $this->title = '商家详情';
        $id=$this->request->param('id');

        //个人信息
        $userinfo=DB::name($this->table)->where('id',$id)->find();
        if($userinfo['type']==0){
            $userinfo['type']="未认证";
        }elseif ($userinfo['type']==1) {
            $userinfo['type']="个人";
        }else{
            $userinfo['type']="商家";
        }
        if($userinfo['status']==0){
            $userinfo['status']="未提交";
        }elseif ($userinfo['status']==1) {
            $userinfo['status']="待认证";
        }elseif ($userinfo['status']==2) {
            $userinfo['status']="认证通过";
        }else{
            $userinfo['status']="拒绝";
        }
        $userinfo['province']=DB::name("Area")->where('area_id',$userinfo['provinceid'])->value("area_name");
        $userinfo['city']=DB::name("Area")->where('area_id',$userinfo['cityid'])->value("area_name");
        $userinfo['county']=DB::name("Area")->where('area_id',$userinfo['areaid'])->value("area_name");
        $this->assign('userinfo',$userinfo);
        
        $user=DB::name("UserExtendBus")
            ->alias('ue')
            ->where('ue.user_id',$id)
            ->join('User u', 'u.id = ue.user_id')
            ->field('ue.* ,u.email')
            ->find();

        $this->assign('extend',$user);

        $this->fetch();
    }

    /**
     * 商家积分
     * @auth true
    */
    public function integral(){

        $this->title = '商家积分详情';

        $userid = $this->request->param('id', 0);
        $credits=DB::name("UserWallet")->where('user_id',$userid)->find();
        //$credits['available']=$credits['credits']-$credits['clock_credits']-$credits['used_credits'];
        $this->assign('credits',$credits);

        $query = $this->_query("UserCreditsLog")
            ->where('user_id',$userid)
            ->like('type')
            ->equal('kind');
        $query->order('id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _integral_page_filter(&$data)
    {

        foreach ($data as &$vo) {
            $vo['kindtype'] = TransItem::transCreditsOption(Arr::get($vo, 'kind'));
            $vo['add_time']=date("Y-m-d",$vo['created_at']);
        }
    }

     /**
     * 商家积分扣除
     * @auth true
     */
    public function deduction(){

        if ($this->request->isGet()) {

           $this->title = '会员积分扣除';
           $user_id= $this->request->param('id', 0);
           $this->assign('user_id',$user_id);
           $this->fetch();

        }else{

           $data=$this->request->post();
           if(!preg_match("/^[1-9][0-9]*$/",$data['num'])){
                $this->error('只能输入整数');
           }else{

                $user_wallet=DB::name('user_wallet')->where('user_id',$data['user_id'])->find();
                if($data['num']>$user_wallet['credits']){
                    $this->error('用户积分不足');
                }else{
                    Db::startTrans();
                    $updateData['credits'] = Db::raw('credits - ' . $data['num']);
                    $result = Db::name('user_wallet')
                        ->where('user_id', $data['user_id'])
                        ->update($updateData);
                    if($result){
                        $result=Db::name('user_credits_log')
                                ->insertGetId([
                                    'user_id'           => $data['user_id'],
                                    'credits'           => $data['num'],
                                    'type'              => 2,
                                    'kind'              => 7,
                                    'created_at'        => time(),
                                    'updated_at'        => time()
                                ]);
                        if($result){
                            Db::commit();
                            $this->success('积分扣除成功！');
                        }else{
                            Db::rollback();
                            $this->error('积分扣除失败');
                        }
                    }else{
                        Db::rollback();
                        $this->error('积分扣除失败');
                    }
                }
                
           }

        }
    }


    /**
     * 商家充值记录
     * @auth true
    */
    public function charge(){

        $this->title = '商家发布豆详情';

        $userid = $this->request->param('id', 0);
        $credits=DB::name("UserWallet")->where('user_id',$userid)->find();
        $credits['available']=$credits['publish_bean']-$credits['clock_publish_bean']-$credits['used_publish_bean'];
        $this->assign('credits',$credits);

        $query = $this->_query("UserRechargeLog")
            ->where('user_id',$userid)
            ->like('type')
            ->equal('kind');
        $query->order('id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _charge_page_filter(&$data)
    {

        foreach ($data as &$vo) {
            $vo['status']=DB::name("UserCharge")->where('id',$vo['charge_id'])->value("status");
            $vo['add_time']=date("Y-m-d H:i:s",$vo['created_at']);
        }

    }


    /**
     * 兼职记录
     * @auth true
    */
    public function jobRecord(){

        $this->title="兼职记录";

        $userid = $this->request->param('id', 0);
        $this->assign('userid',$userid);

        $query = $this->_query("JobsApply")
            ->where('user_id',$userid)
            ->like('status')
            ->equal('status');
        $query->order('id desc')->page();

    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _jobRecord_page_filter(&$data)
    {

        foreach ($data as &$vo) {
            $vo['jtitle']=DB::name("Jobs")->where('id',$vo['job_id'])->value("title");
            $vo['add_time']=date("Y-m-d",$vo['created_at']);
            $vo['jstatus'] = TransItem::transJobApply(Arr::get($vo, 'status'));
        }


    }
     /**
     * 兼职发布记录
     * @auth true
    */
    public function pubjobRecord(){

        $this->title="兼职发布记录";

        $userid = $this->request->param('id', 0);
        $this->assign('userid',$userid);

        $query = $this->_query("Jobs")
            ->where('user_id',$userid)
            ->field("created_at,title,status,work_type,work_time")
            ->like('title')
            ->equal('type');
        $query->order('id desc')->page();
    }

     /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _pubjobRecord_page_filter(&$data)
    {

        foreach ($data as &$vo) {
            
            $vo['add_time']=date("Y-m-d",$vo['created_at']);
            $vo['worktime']=date("Y-m-d",$vo['work_time']);
            $vo['jstatus'] = TransItem::transJobPassStatus(Arr::get($vo, 'status'));
            $vo['worktype']=DB::name("JobsType")->where('id',$vo['work_type'])->value("name");
        }




    }






}
